package com.lambda.widgets.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.view.View;

/**
 * 绘制badge的工具
 * <p>
 * class MyView extends View {
 * private badge;
 * ...
 * onDraw(Canvas canvas) {
 * badge.draw(canvas);
 * }
 * }
 * </p>
 */
public class Badge {
    private final View target;
    private final Paint paint;
    private final Paint paint2;
    private RectF rectF;
    private String text;
    private float hp = 0.8f;
    private float vp = 0.1f;
    private int radius = 22;
    private int backgroundColor = Color.RED;
    private Bitmap bitmap;
    private NinePatch ninePatch;

    public Badge(View target) {
        if (target == null)
            throw new NullPointerException("target can't be null");

        this.target = target;
        Resources resources = target.getResources();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(backgroundColor);
        paint.setStyle(Paint.Style.FILL);

        paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setColor(Color.WHITE);
        paint2.setStyle(Paint.Style.FILL);
        paint2.setTextAlign(Paint.Align.CENTER);
        paint2.setTextSize(resources.getDisplayMetrics().density * 10);
    }

    public void setText(String text) {
        this.text = text;
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setPosition(float hp, float vp) {
        this.hp = hp;
        this.vp = vp;
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setRadius(int radius) {
        this.radius = radius;
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setTextSize(int textSize) {
        paint2.setTextSize(textSize);
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setTextColor(int textColor) {
        paint2.setColor(textColor);
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        paint.setColor(backgroundColor);
        ViewCompat.postInvalidateOnAnimation(target);
    }

    public void setBackground(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public void draw(Canvas canvas) {
        if (canvas == null) return;

        final int width = target.getMeasuredWidth();
        final int height = target.getMeasuredHeight();
        final int xp = (int) (width * hp);
        final int yp = (int) (height * vp);

        if (!TextUtils.isEmpty(text)) {
            if (rectF == null) {
                rectF = new RectF();
            }

            Paint.FontMetrics fontMetrics = paint2.getFontMetrics();
            final float fontWidth = paint2.measureText(text);
            final float fontHeight = fontMetrics.bottom - fontMetrics.top;
            rectF.left = xp - (fontWidth + fontHeight) / 2;
            rectF.top = yp - fontHeight / 2;
            rectF.right = rectF.left + fontWidth + fontHeight;
            rectF.bottom = rectF.top + fontHeight;

            if (bitmap == null) {
                canvas.drawRoundRect(rectF, fontHeight / 2, fontHeight / 2, paint);
            } else {
                byte[] chunk = bitmap.getNinePatchChunk();
                if (NinePatch.isNinePatchChunk(chunk)) {
                    if (ninePatch == null) {
                        ninePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk(), text);
                    }
                    ninePatch.draw(canvas, rectF);
                } else {
                    canvas.drawBitmap(bitmap, null, rectF, null);
                }
            }

            canvas.drawText(text, xp, rectF.top + fontMetrics.leading - fontMetrics.top, paint2);
        } else {
            canvas.drawCircle(xp, yp, radius, paint);
        }
    }
}
